#ifndef PLAYER_H
#define PLAYER_H

#include "spritex.h"
#include "spritexanimation.h"
#include "character.h"
#include <string>
#include <stdint.h>
#include <map>
#include <list>

using std::string;
using std::map;
using std::list;

struct MoveState {
    uint32_t jump:1;
    uint32_t walk:1;
    uint32_t faststop:1;
    uint32_t stand_static:1;
    uint32_t swimming:1;
    uint32_t turn:1;
    uint32_t squat:1;
    uint32_t bigger:1;
    uint32_t smaller:1;
    uint32_t speeding:1;
    uint32_t slowdown:1;
    uint32_t facetoleft:1;
    uint32_t attack:1;
};

struct KeyState {
    uint32_t jump:4;
    uint32_t left:4;
    uint32_t right:4;
    uint32_t fire:4;
    uint32_t squat:4;
};

struct ActionState {
    uint32_t curraction:16;
    uint32_t newaction:16;
};

struct AttrState {
    uint32_t hasWeapon:1;
    uint32_t growup:1;
};

struct MoveRange {
    float top;
    float bottom;
    float left;
    float right;
};

class Player : public Sprite
{
public:
    Player();
    ~Player();
    CREATE_FUNC(Player)
    bool loadResources(const string& spritexfile,const string&fn);
public:

    enum class DirectState {
        LEFT=0x1,
        RIGHT=0x2
    };

    enum class ActionType:char {
        WALK_BIG, JUMP_BIG, RUN_BIG, SWIMMING_BIG,
        SQUAT_BIG, STATIC_BIG, GETFLAG_BIG, FASTSTOP_BIG,

        WALK_BIG_W, JUMP_BIG_W, RUN_BIG_W, SWIMMING_BIG_W,
        SQUAT_BIG_W, STATIC_BIG_W, GETFLAG_BIG_W, FASTSTOP_BIG_W,

        WALK_SMALL, JUMP_SMALL, SWIMMING_SMALL,
        STATIC_SMALL, GETFLAG_SMALL, FASTSTOP_SMALL,

        BIGGER, SMALLER, DIE,

        NONE
    };
    void actionChange(void);
    void listenKeyboardEvent(void);
    void initControlConfig(void);

    void handleKeyPressedEvent(EventKeyboard::KeyCode,Event*);
    void handleKeyReleasedEvent(EventKeyboard::KeyCode,Event*);

    void checkMoveRange(void* levelMap,list<Sprite*>& objList);

    void updatePosition(float);
    void updateMoveState(void);
    void updateActionState(void);
    void adjustPosition(void);
    void update(float delta);

    void doMove(void);
    void doAction(void);

    ActionType choiceAction(void);

public:
    float hspeed;
    float vspeed;
    float hacceleration;
    float vacceleration;
    float hmaxspeed;
    float vmaxspeed;
    Vec2 move;
    MoveRange moveRange;
    KeyState keyState;
    MoveState moveState;
    DirectState directState;
    AttrState attrState;
    uint32_t inputtype;
    ActionState actionState;
    map<string,EventKeyboard::KeyCode> keyMap;

public:
    Animate* action[25];
};

#endif // PLAYER_H
